import resp from './resp'
const jwt = require('jsonwebtoken')
import * as postFetch from './post'
import * as getFetch from './get'

export const validateToken = async (event) => {
  try {
    const token = await event.sender.executeJavaScript(`
      localStorage.getItem('zg-jwt')
    `);

    if (!token) return false;

    return jwt.verify(token, import.meta.env.VITE_SECRET_KEY);
  } catch (error) {
    return null;
  }
}
export const get = async (event, tokenInfo, requestPayload) => {
  let response = {
    code: 400
  }
  const {action, data} = requestPayload

  try {
    const actionList = {
      assets: async () => {
        response = resp.success(await getFetch.fetchAssets.call(tokenInfo, data))
      },
      subclass: async () => {
        response = resp.success(await getFetch.fetchSubclass.call(tokenInfo, data))
      },
      accountList: async () => {
        response = resp.success(await getFetch.fetchAccountList.call(tokenInfo, data))
      },
      accountInfo: async () => {
        response = resp.success(await getFetch.fetchAccountInfo.call(tokenInfo, data))
      },
      transactionList: async () => {
        response = resp.success(await getFetch.fetchTransactionList.call(tokenInfo, data))
      },
      assetStatistics: async () => {
        response = resp.success(await getFetch.fetchAssetStatistics.call(tokenInfo, data))
      },
      assetChartData: async () => {
        response = resp.success(await getFetch.fetchAssetChartData.call(tokenInfo, data))
      },
      assetAllChartData: async () => {
        response = resp.success(await getFetch.fetchAssetAllChartData.call(tokenInfo, data))
      }
    }

    actionList[action] && await actionList[action]()
  } catch (error) {
    resp.error(error)
  }

  return response;
}

export const post = async (event, tokenInfo, requestPayload) => {
  let response = {
    code: 400
  }
  const {action, data} = requestPayload

  try {
    const actionList = {
      login: async () => {
        try {
          const token = await postFetch.fetchLogin.call(tokenInfo, data)
          if (!token) {
            response = resp.error('账号密码错误')
          } else {
            response = resp.success({
              token
            })
          }
        } catch (error) {
          response = resp.error('登录失败')
        }
      },
      register: async () => {
        try {
          const token = await postFetch.fetchRegister.call(tokenInfo, data)

          response = resp.success({
            token
          })
        } catch (error) {
          response = resp.error(error)
        }
      },
      createAccount: async () => {
        try {
          const id = await postFetch.fetchCreateAccount.call(tokenInfo, data)

          response = resp.success({
            id
          })
        } catch (error) {
          response = resp.error(error)
        }
      },
      fetchTransferBalance: async () => {
        try {
          const id = await postFetch.fetchTransferBalance.call(tokenInfo, data)
          console.log('fetchTransferBalance', id)

          response = resp.success({
            id
          })
        } catch (error) {
          response = resp.error(error)
        }
      }
    }

    actionList[action] && await actionList[action]()
  } catch (error) {
    resp.error(error)
  }

  return response;
}